package com.cxs.drawpointdemo.point;

import com.cxs.drawpointdemo.constant.Common;
import com.cxs.drawpointdemo.util.KvStoreUtils;
import com.cxs.drawpointdemo.util.ZSONUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import ohos.data.distributed.user.SingleKvStore;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.List;

public class DrawPoint extends Component implements Component.DrawTask,
        Component.TouchEventListener {

    List<MyPoint> pointList = new ArrayList<>();
    MyPoint myPoint = new MyPoint();
    Paint paint;
    private boolean isGuessPlay = false;


    public DrawPoint(Context context) {
        super(context);
        initComponent();
    }

    public DrawPoint(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initComponent();
    }

    private void initComponent() {
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(10);
        addDrawTask(this);
        setTouchEventListener(this);
    }

    public void setGuessPlay(boolean guessPlay) {
        isGuessPlay = guessPlay;
    }

    public void setPointList(List<MyPoint> pointList) {
        this.pointList = pointList;
        invalidate();
    }

    /**
     * 绘图任务
     * @param component
     * @param canvas
     */
    @Override
    public void onDraw(Component component, Canvas canvas) {
        Point lastPoint = null;
        /*
            遍历所有点，判断该点是否为最后一个点，如果不是，将这个点与之前的点连接组成线条
         */
        for (MyPoint point : pointList) {
            if (lastPoint != null) {
                canvas.drawLine(lastPoint, new Point(point.getPosX(), point.getPosY()), paint);
            }
            if (point.isLastPoint()) {
                lastPoint = null;
            } else {
                lastPoint = new Point(point.getPosX(), point.getPosY());
            }
        }
    }

    /**
     * 触屏事件
     * @param component
     * @param touchEvent
     * @return
     */
    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (isGuessPlay) {
            return false;
        }
        float posX = touchEvent.getPointerPosition(touchEvent.getIndex()).getX();
        float posY = touchEvent.getPointerPosition(touchEvent.getIndex()).getY();

        if (touchEvent.getAction() == TouchEvent.POINT_MOVE) {
            pointList.add(new MyPoint(posX, posY));
        }
        if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_UP) {
            pointList.add(new MyPoint(posX, posY, true));
            // 将坐标集合转换为ZSON字符串
            String zsonStr = ZSONUtils.toString(pointList);
            SingleKvStore kvStore = KvStoreUtils.getKvStore();
            kvStore.putString(Common.POINT_LIST_KEY, zsonStr);
        }
        // 更新绘图任务
        invalidate();
        return true;
    }

}
